package com.qfedu.fmmall.sign.service.impl;

import com.qfedu.fmmall.common.status.ResStatus;
import com.qfedu.fmmall.common.utils.MD5Utils;
import com.qfedu.fmmall.common.vo.ResultVO;
import com.qfedu.fmmall.entity.Users;
import com.qfedu.fmmall.sign.service.UserSignUpService;
import com.qfedu.fmmall.sign.service.feign.UserLoginClient;
import com.qfedu.fmmall.sign.service.feign.UserSignUpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : yanBin
 * @date :2022/1/22 14:35
 */
@Service
public class UserSignUpServiceImpl implements UserSignUpService {
    @Autowired
    private UserSignUpClient userSignUpClient;
    @Autowired
    private UserLoginClient userLoginClient;

    @Override
    public ResultVO userRegistration(Users users) {
        try {
            List<Users> usersList = userLoginClient.getCheck(users.getUsername());
            if (usersList != null && usersList.size() <= 0) {
                return new ResultVO(ResStatus.NO.ordinal(), "服务繁忙，请重新访问", null);
            }
            if (usersList != null && usersList.size()>0){
                return new ResultVO(ResStatus.ALREADY_EXISTS.getCode(), "用户已经存在", null);
            }
            String md5Password = MD5Utils.md5(users.getPassword());
            users.setPassword(md5Password);
            users.setUserImg("img/default.png");
            ResultVO resultVO = userSignUpClient.userRegistration(users);
            return resultVO;
        } catch (Exception e) {
            return new ResultVO(ResStatus.NO.ordinal(), "注册失败", null);
        }

    }
}
